//
//  MenuScene.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "MenuScene.h"
#import "LevelChoosingScene.h"
#import "Setting.h"
#import "InfoScene.h"
#import "TutorialScene.h"
#import "CalibratePopup.h"
#import "Utility.h"
#import "MovieScene.h"

@implementation MenuScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    MenuScene *layer = [MenuScene node];
    [scene addChild:layer];
    return scene;
}

-(id) init
{
    if (self = [super init])
    {
//        [[CCDirector sharedDirector] setDisplayStats:NO];
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        CCSprite *sprTitle = [CCSprite spriteWithSpriteFrameName:SFN_TITLE_GAME];
        [sprTitle setAnchorPoint:ccp(0.5, 1)];
        [sprTitle setPosition:ccp(winSize.width / 2, winSize.height - uniPos(20, 30))];
        [self addChild:sprTitle];
        
        mMenu = [[CCMenu alloc] init];
        [mMenu setPosition:CGPointZero];
        [self addChild:mMenu];
        
        // play
        CCSprite *sprBtnPlay = [CCSprite spriteWithSpriteFrameName:SFN_BTN_PLAY];
        CCSprite *sprBtnPlayHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_PLAY];
        [sprBtnPlayHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMIPlay = [CCMenuItemSprite itemWithNormalSprite:sprBtnPlay
                                                      selectedSprite:sprBtnPlayHover
                                                              target:self
                                                            selector:@selector(onPlay)];
        
        
        
        // twitter
        CCSprite *sprBtnTwitter = [CCSprite spriteWithSpriteFrameName:SFN_BTN_TWITTER];
        CCSprite *sprBtnTwitterHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_TWITTER];
        [sprBtnTwitterHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMITwitter = [CCMenuItemSprite itemWithNormalSprite:sprBtnTwitter
                                                         selectedSprite:sprBtnTwitterHover
                                                                 target:self
                                                               selector:@selector(onTwitter)];
        
        
        
        // facebook
        CCSprite *sprBtnFacebook = [CCSprite spriteWithSpriteFrameName:SFN_BTN_FACEBOOK];
        CCSprite *sprBtnFacebookHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_FACEBOOK];
        [sprBtnFacebookHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMIFacebook = [CCMenuItemSprite itemWithNormalSprite:sprBtnFacebook
                                                          selectedSprite:sprBtnFacebookHover
                                                                  target:self
                                                                selector:@selector(onFacebook)];

        
        // info
        CCSprite *sprBtnInfo = [CCSprite spriteWithSpriteFrameName:SFN_BTN_INFO];
        CCSprite *sprBtnInfoHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_INFO];
        [sprBtnInfoHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMIInfo = [CCMenuItemSprite itemWithNormalSprite:sprBtnInfo
                                                      selectedSprite:sprBtnInfoHover
                                                              target:self
                                                            selector:@selector(onInfo)];

        
        
        // sound
        CCSprite *sprBtnSoundOn = [CCSprite spriteWithSpriteFrameName:SFN_BTN_SOUND_ON];
        CCSprite *sprBtnSoundOnHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_SOUND_ON];
        [sprBtnSoundOnHover setColor:CL_PANEL_BTN];
        
        CCSprite *sprBtnSoundOff = [CCSprite spriteWithSpriteFrameName:SFN_BTN_SOUND_OFF];
        CCSprite *sprBtnSoundOffHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_SOUND_OFF];
        [sprBtnSoundOffHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMISoundOn = [CCMenuItemSprite itemWithNormalSprite:sprBtnSoundOn
                                                         selectedSprite:sprBtnSoundOnHover];
        CCMenuItem *mMISoundOff = [CCMenuItemSprite itemWithNormalSprite:sprBtnSoundOff
                                                          selectedSprite:sprBtnSoundOffHover];
        
        mMISoundOn.tag = 1;
        mMISoundOff.tag = 0;
        CCMenuItemToggle *mMISound = [CCMenuItemToggle itemWithTarget:self selector:@selector(onSound:) items:mMISoundOn, mMISoundOff, nil];
        if (![Setting getInstance]._isSoundFX)
            mMISound.selectedIndex = 1;
        
        
        // music
        CCSprite *sprBtnMusicOn = [CCSprite spriteWithSpriteFrameName:SFN_BTN_MUSIC_ON];
        CCSprite *sprBtnMusicOnHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_MUSIC_ON];
        [sprBtnMusicOnHover setColor:CL_PANEL_BTN];
        
        CCSprite *sprBtnMusicOff = [CCSprite spriteWithSpriteFrameName:SFN_BTN_MUSIC_OFF];
        CCSprite *sprBtnMusicOffHover = [CCSprite spriteWithSpriteFrameName:SFN_BTN_MUSIC_OFF];
        [sprBtnMusicOffHover setColor:CL_PANEL_BTN];
        CCMenuItem *mMIMusicOn = [CCMenuItemSprite itemWithNormalSprite:sprBtnMusicOn
                                                         selectedSprite:sprBtnMusicOnHover];
        CCMenuItem *mMIMusicOff = [CCMenuItemSprite itemWithNormalSprite:sprBtnMusicOff
                                                          selectedSprite:sprBtnMusicOffHover];

        mMIMusicOn.tag = 1;
        mMIMusicOff.tag = 0;
        CCMenuItemToggle *mMIMusic = [CCMenuItemToggle itemWithTarget:self selector:@selector(onMusic:) items:mMIMusicOn, mMIMusicOff, nil];
        if (![Setting getInstance]._isSoundBG)
            mMIMusic.selectedIndex = 1;
        
        mMIInfo.activeArea = CGRectMake(-10, -20, mMIInfo.contentSize.width + 20, mMIInfo.contentSize.height + 50);
        mMIMusic.activeArea = CGRectMake(-10, -20, mMIMusic.contentSize.width + 20, mMIMusic.contentSize.height + 50);
        mMISound.activeArea = CGRectMake(-10, -20, mMISound.contentSize.width + 20, mMISound.contentSize.height + 50);
        mMIFacebook.activeArea = CGRectMake(-10, -20, mMIFacebook.contentSize.width + 20, mMIFacebook.contentSize.height + 50);
        mMITwitter.activeArea = CGRectMake(-10, -20, mMITwitter.contentSize.width + 20, mMITwitter.contentSize.height + 50);
        
        [mMenu addChild:mMIPlay];
        [mMenu addChild:mMITwitter];
        [mMenu addChild:mMIFacebook];
        [mMenu addChild:mMIInfo];
        [mMenu addChild:mMISound];
        [mMenu addChild:mMIMusic];
        
        [mMIPlay setPosition:ccp(winSize.width / 2, winSize.height / 2)];
        [mMITwitter setPosition:uniPos(ccp(winSize.width - 30, 40), ccp(winSize.width - 60, 60))];
        [mMIFacebook setPosition:uniPos(ccp(winSize.width - 80, 40), ccp(winSize.width - 160, 60))];
        [mMIInfo setPosition:uniPos(ccp(30, 40), ccp(60, 60))];
        [mMIMusic setPosition:uniPos(ccp(80, 40), ccp(160, 60))];
        [mMISound setPosition:uniPos(ccp(130, 40), ccp(260, 60))];
        
        self.touchEnabled = YES;
    }
    
    return self;
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:TOUCH_PRIORITY_MAIN_UI swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return NO;
}

-(void) onPlay
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    if ([defaults objectForKey:KEY_FIRST_TIME_PLAY] == nil)
//    {
//        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[TutorialScene scene]]];
//    }
//    else
    {
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[LevelChoosingScene scene]]];
    }
}

-(void) onTwitter
{
    CCLOG(@"onTwitter");
    NSURL *urlFanpage = [NSURL URLWithString:@"twitter://user?screen_name=pixeptionteam"];
    if (![[UIApplication sharedApplication] openURL: urlFanpage])
    {
        NSURL *webURL = [NSURL URLWithString:@"https://twitter.com/pixeptionteam"];
        [[UIApplication sharedApplication] openURL: webURL];
    }
}

-(void) onFacebook
{
    CCLOG(@"onFacebook");
    NSURL *urlFanpage = [NSURL URLWithString:@"fb://profile/260354354126460"];
    if (![[UIApplication sharedApplication] openURL: urlFanpage])
    {
        NSURL *webURL = [NSURL URLWithString:@"http://www.facebook.com/260354354126460"];
        [[UIApplication sharedApplication] openURL: webURL];
    }
}

-(void) onInfo
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[InfoScene scene]]];
//    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[MovieScene scene]]];
}

-(void) onSound:(id)sender
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    CCMenuItemToggle *item = (CCMenuItemToggle *)(sender);
    if (item.selectedItem.tag == 1)
    {
        [Setting getInstance]._isSoundFX = YES;
    }
    else
    {
        [Setting getInstance]._isSoundFX = NO;
    }
}

-(void) onMusic:(id)sender
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    CCMenuItemToggle *item = (CCMenuItemToggle *)(sender);
    if (item.selectedItem.tag == 1)
    {
        [Setting getInstance]._isSoundBG = YES;
    }
    else
    {
        [Setting getInstance]._isSoundBG = NO;
    }
}

-(void) dealloc
{
    CCLOG(@"MenuScene dealloc!");
    [mMenu release];
    mMenu = nil;
    
    [super dealloc];
}

@end
